import React, {Component} from 'react'
import {routerShape} from 'react-router';
import {inject, observer} from 'mobx-react';
import {observable, action, toJS} from 'mobx';
import {WeaTimeline} from '../../../Component/index'
import { Spin} from 'antd';
const {WeaItem} = WeaTimeline

import ComVar from '../ComVar'

const {AppType, WeiboType} = ComVar

import WeaUtils from '../../../Component/wea-utils'

const {DateFormat} = WeaUtils

import '../../../../style/myBlog/rightContainer/weibo.less'

import WeaSplitLine from "../../../Component/wea-split-line"
import WeaWhitePage from "../../../Component/wea-white-page"

import WeiboContainer from './WeiboContainer'
import WeiboEdit from './WeiboEdit'
import WeiboNormal from './WeiboNormal'
import WeiboNormalPermissions from './WeiboNormalPermissions'
import WeiboExpired from './WeiboExpired'
import WeiboWhite from './WeiboWhite'

import {WeaLocaleProvider,WeaAlertPage} from "ecCom";
import WeiboEcmeEdit from "./WeiboEcmeEdit";

const getLabel = WeaLocaleProvider.getLabel;

@inject('blogMyBlog')
@observer

class Weibos extends Component {
  static contextTypes = {
    router: routerShape
  }

  constructor(props) {
    super(props)
  }

  render() {
    const {records, blogMyBlog, tindex,stype,userInfo,addMore,hasMore, weiboCKEditorConfig, blogType, hasScroll, hasEditor, hasImage, defaultTemplate, height, keyWords,moreLoading} = this.props,
      {currentUser = {}} = userInfo,
      {
        discussList = [],
        isManagerScore = 0,
        isMood = 0,
        oneSubmit = 0,
      } = records


    let hasSubmited = true
    discussList.forEach(
      item => {
        if (item.workdateLocal == DateFormat(new Date(), "yyyy-MM-dd"))
          if (item.type == WeiboType.white) {
            hasSubmited = false
            return
          }
      }
    )
      let paaa = {};
    let weibos = []
    if (hasEditor) weibos.push({type: WeiboType.current})
    if (discussList[0]) {
      const {workdate, workdateLocal, type} = discussList[0];
      //分页绑定的日期 以及类型页面
      paaa={workDate:workdate,type:stype};

      if (hasEditor) weibos[0] = {type: WeiboType.current, workdate: workdate, workdateLocal: workdateLocal};
      if (workdateLocal == DateFormat(new Date(), "yyyy-MM-dd") && (type != WeiboType.normal) && hasEditor) {
        weibos = weibos.concat(discussList.slice(1))
      } else {
        weibos = weibos.concat(discussList)
      }
    }

    const content = this.renderWeiboDom(blogMyBlog, weibos, currentUser, isManagerScore, isMood, blogType, hasScroll, hasEditor, hasImage, hasSubmited, defaultTemplate, weiboCKEditorConfig, keyWords,oneSubmit)


     
    return (
      hasEditor ? (
        <div className="wea-myBlog-weibo" style={{backgroundColor: "#f4f4f4"}}>
          {
            hasScroll ? (
              <WeaTimeline ecId={`${this && this.props && this.props.ecId || ''}_WeaTimeline@lnwm68`}>{content}</WeaTimeline>
            ) : (
              <div className="weiboNoTime">
                {
                  (weibos.length > 1) ? content : <WeaWhitePage ecId={`${this && this.props && this.props.ecId || ''}_WeaWhitePage@ocvzez`} height={height}/>
                }
                  {
                      (hasMore>0) ?  (
                            moreLoading?<div style={{width: "100%", "text-align": "center", height: 40,
                                "font-size": 13,
                                "background-color":" rgb(255, 255, 255)",
                                "font-family":"-webkit-body",
                                "line-height": 40,
                                "color": "#525050",
                                "cursor": "pointer",
                                "border-top": "1px solid #e0dede"}} onClick={() => {
                                addMore(paaa,tindex)
                            }}><Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@fd9d8p`} /></div>:<div style={{width: "100%", "text-align": "center", height: 40,
                                "font-size": 13,
                                "background-color":" rgb(255, 255, 255)",
                                "font-family":"-webkit-body",
                                "line-height": 40,
                                "color": "#525050",
                                "cursor": "pointer",
                                "border-top": "1px solid #e0dede"}} onClick={() => {
                                addMore(paaa,tindex)
                            }}>{getLabel(82720,'加载更多')}</div>
                        ):""
                  }
              </div>
            )
          }
        </div>
      ) : (
        weibos.length > 0 ? (
          <div className="wea-myBlog-weibo" style={{backgroundColor: "#f4f4f4"}}>
            {
              hasScroll ? (
                <WeaTimeline ecId={`${this && this.props && this.props.ecId || ''}_WeaTimeline@8orwhs`}>{content}</WeaTimeline>
              ) : (
                <div className="weiboNoTime">{content}
                    {
                        (hasMore>0) ? (
                            moreLoading?<div style={{width: "100%", "text-align": "center", height: 40,
                                "font-size": 13,
                                "background-color":" rgb(255, 255, 255)",
                                "font-family":"-webkit-body",
                                "line-height": 40,
                                "color": "#525050",
                                "cursor": "pointer",
                                "border-top": "1px solid #e0dede"}} onClick={() => {
                                addMore(paaa,tindex)
                            }}><Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@nf1dfd`} /></div>:<div style={{width: "100%", "text-align": "center", height: 40,
                                "font-size": 13,
                                "background-color":" rgb(255, 255, 255)",
                                "font-family":"-webkit-body",
                                "line-height": 40,
                                "color": "#525050",
                                "cursor": "pointer",
                                "border-top": "1px solid #e0dede"}} onClick={() => {
                                addMore(paaa,tindex)
                            }}>{getLabel(82720,'加载更多')}</div>
                        ):""
                    }
                </div>
              )
            }
          </div>
        ) : <WeaWhitePage ecId={`${this && this.props && this.props.ecId || ''}_WeaWhitePage@8ywrdg`} height={height}/>
      )
    )
  }

  renderWeiboDom = (blogMyBlog, weibos, currentUser, isManagerScore, isMood, blogType, hasScroll, hasEditor, hasImage, hasSubmited, defaultTemplate, weiboCKEditorConfig, keyWords,oneSubmit) => {
    let preWorkData = "";
    return weibos.map(
      (item, key) => {
        let {
            type, workdate = "", ismerge ,createdate = "", createtime = "", workdateLocal = "", createdateLocal = "", createtimeLocal = "", content, username, userid, imageurl, score,
            isCanEdit,isCanCom, isCanReplenish, isUnSumitRemind, isShowLog, isSpread,isCanZan, isMeCanScored, isShowReadOrUnread, isReaded, replenishTitle, isHasLocation,specialStatus,
            blogZanBean, blogReplyList = [], appItems = [], isShowBdShare = 0, sysWorkLog = [],isPermissions,partid=0,ecmeEditButtons=[],shareType = 1,shareValue = ''
                } = item,
          user = {name: username, id: userid, src: imageurl},
          blogId = item.id,
          date = createdate && createtime ? (createdate + " " + createtime) : "",
          dateLocal = createdateLocal && createtimeLocal ? (createdateLocal + " " + createtimeLocal) : "",
          isLast = key == weibos.length - 1 ? true : false,
          moods = [],
          agrees = blogZanBean ? (blogZanBean.blogZanPoList ? blogZanBean.blogZanPoList : []) : [],
          isAgree = false

        appItems.forEach(
          item => {
            if (item && item.type == AppType.mood) {
              moods.push(item);
            }
          }
        )

        agrees.forEach(
          item => {
            if (item.zanHrmid == currentUser.id) isAgree = true
          }
        )

          let showDot = "hhh:"+workdateLocal;

         if(key<weibos.length){

             if(blogType=='201'){
                 if(workdateLocal==DateFormat(new Date(), "yyyy-MM-dd")){
                     if(weibos[key-2]&&weibos[key-2].workdateLocal&&workdateLocal==weibos[key-2].workdateLocal){
                         showDot = "";
                     }
                 }else{
                     if(weibos[key-1]&&weibos[key-1].workdateLocal&&workdateLocal==weibos[key-1].workdateLocal){
                         showDot = "";
                     }
                 }
             }else if(blogType=='100'){
                     if(weibos[key-1]&&weibos[key-1].workdateLocal&&workdateLocal==weibos[key-1].workdateLocal){
                         showDot = "";
                     }
             }
         }


        const renderContent = COM => {
          return (
            <WeiboContainer ecId={`${this && this.props && this.props.ecId || ''}_WeiboContainer@dvr3ua@${key}`}
              _key={key}
              partid={partid}
              specialStatus={specialStatus}
              blogMyBlog={blogMyBlog}
              blogId={blogId}
              blogType={blogType}
              isAgree={isAgree}
              agrees={agrees}
              ismerge={ismerge}
              ecmeEditButtons={ecmeEditButtons}
              content={content}
              isShowBdShare={isShowBdShare}
              currentUser={currentUser}
              user={user}
              date={date}
              dateLocal={dateLocal}
              mood={moods[0]}
              type={type}
              shareType={shareType}
              shareValue={shareValue}
              createdate={createdate}
              workdate={workdate}
              score={score * 1}
              defaultTemplate={defaultTemplate}
              isManagerScore={isManagerScore * 1}
              isCanZan={isCanZan * 1}
              isLast={isLast * 1}
              isCanEdit={isCanEdit * 1}
              isCanCom={isCanCom * 1}
              isShowLog={isShowLog * 1}
              isCanReplenish={isCanReplenish * 1}
              isUnSumitRemind={isUnSumitRemind * 1}
              isMeCanScored={isMeCanScored * 1}
              isShowReadOrUnread={isShowReadOrUnread * 1}
              isReaded={isReaded * 1}
              comments={blogReplyList || []}
              systemWorkLog={sysWorkLog || {}}
              hasEditor={hasEditor}
              hasImage={hasImage}
              hasScroll={hasScroll}
              weiboCKEditorConfig={weiboCKEditorConfig}
              replenishTitle={replenishTitle}
              isHasLocation={isHasLocation}
              isPermissions = {isPermissions}
              wworkRecords={sysWorkLog}
              isSpread={isSpread}
              oneSubmit = {oneSubmit}
            >
              <COM ecId={`${this && this.props && this.props.ecId || ''}_COM@4v7qil@${key}`} data={content} id={blogId} keyWords={keyWords} user={user}  />
            </WeiboContainer>
          )
        }

        if (hasScroll) {
          if(isPermissions==0){
            return (
              <WeaItem ecId={`${this && this.props && this.props.ecId || ''}_WeaItem@v7u5ru@${key}`}
                date={workdateLocal}
                type={type}
                showDot={showDot}
                tipDate={createdateLocal + " " + createtimeLocal}
              >
               {renderContent(WeiboNormalPermissions)}
              </WeaItem>
            )
          }else{
            if(this.props.ecmeType=="1"){
              return (

                  <WeaItem ecId={`${this && this.props && this.props.ecId || ''}_WeaItem@k6mfih@${key}`}
                date={workdateLocal}
                type={type}
                showDot={showDot}
                tipDate={createdateLocal + " " + createtimeLocal}
                >
                {type == WeiboType.current && hasEditor  ?
                    <WeiboEcmeEdit ecId={`${this && this.props && this.props.ecId || ''}_WeiboEcmeEdit@8anf2a`}
                        workdate={workdate}
                        blogType={blogType}
                        hasSubmited={hasSubmited}
                        defaultTemplate={defaultTemplate}
                        weiboCKEditorConfig={weiboCKEditorConfig}
                    /> : null}
                {type == WeiboType.normal ? renderContent(WeiboNormal) : null}
                {type == WeiboType.expired ? renderContent(WeiboExpired) : null}
                {type == WeiboType.white ? renderContent(WeiboWhite) : null}
            </WeaItem>
              )
            }else{
                return (
                    <WeaItem ecId={`${this && this.props && this.props.ecId || ''}_WeaItem@tudyqt@${key}`}
                        showDot={showDot}
                        date={workdateLocal}
                        type={type}
                        tipDate={createdateLocal + " " + createtimeLocal}
                    >
                        {type == WeiboType.current && hasEditor  ?
                            <WeiboEdit ecId={`${this && this.props && this.props.ecId || ''}_WeiboEdit@mpatxv`}
                                workdate={workdate}
                                blogType={blogType}
                                hasSubmited={hasSubmited}
                                defaultTemplate={defaultTemplate}
                                weiboCKEditorConfig={weiboCKEditorConfig}
                            /> : null}
                        {type == WeiboType.normal ? renderContent(WeiboNormal) : null}
                        {type == WeiboType.expired ? renderContent(WeiboExpired) : null}
                        {type == WeiboType.white ? renderContent(WeiboWhite) : null}
                    </WeaItem>
                )
            }
          }
        } else {
          const renderContents = () => {
            if(isPermissions==0){
              return renderContent(WeiboNormalPermissions)
            }

            if (type == WeiboType.current && hasEditor)
              return (
                <WeiboEdit ecId={`${this && this.props && this.props.ecId || ''}_WeiboEdit@1t77ra@${key}`}
                  workdate={workdate}
                  blogType={blogType}
                  hasSubmited={hasSubmited}
                  defaultTemplate={defaultTemplate}
                  weiboCKEditorConfig={weiboCKEditorConfig}
                />
              )
            else if (type == WeiboType.normal)
              return renderContent(WeiboNormal)
            else if (type == WeiboType.expired)
              return renderContent(WeiboExpired)
            else if (type == WeiboType.white)
              return renderContent(WeiboWhite)
            else return <div/>
          }

          let timeHr = ""
          if (preWorkData !== createdate) {
            preWorkData = createdate
            timeHr = createdate
          }
          //以下是 月 日 提交内容
          let _dateFmt = DateFormat(new Date(createdate), "-M-dd- EE ");
          _dateFmt = _dateFmt.replace('-', getLabel(83170, "以下是")).replace('-', getLabel(33452, "月")).replace('-', getLabel(390, "日")) + getLabel(83171, "提交内容");
          _dateFmt = _dateFmt.replace('周日', getLabel(16106, "周日")).replace('周一', getLabel(16100, "周一")).replace('周二', getLabel(16101, "周二"))
            .replace('周三', getLabel(16102, "周三")).replace('周四', getLabel(16103, "周四")).replace('周五', getLabel(16104, "周五")).replace('周六', getLabel(16105, "周六"));
          return (
            <div>
              {timeHr ? (
                <WeaSplitLine ecId={`${this && this.props && this.props.ecId || ''}_WeaSplitLine@xvcmnt@${key}`}
                  style={(
                    key === 0 ? {
                      borderTop: "none",
                      marginBottom: "16px"
                    } : {borderTop: "1px solid #eaeaea", padding: "8px", height: "48px"}
                  )}
                >
                  {_dateFmt}
                </WeaSplitLine>
              ) : ""}
              {renderContents()}
            </div>
          )
        }
      // // }else{
      //   return (
      //     <div style={{position : 'relative', height : 120}}>
      //       <WeaAlertPage iconSize={100} >
      //          <div style={{color : '#000'}}>
      //               你当前不具有对工作微博的查看权限，请联系对方将微博分享给你，或向他发关注申请 ！
      //           </div>
      //       </WeaAlertPage>
      //     </div>
      //   )
      // // }
      }
    )
  }
}

Weibos.propTypes = {
  userInfo: React.PropTypes.object,//使用currentUser.id属性
  height: React.PropTypes.number,//声明定高
  //isReset: React.PropTypes.bool,//将其置为true，滚动条会置顶，用于切换card时。
  defaultTemplate: React.PropTypes.object,//使用tempContent属性，用于保存之前的编辑内容
  records: React.PropTypes.object,//显示的数据，其中的discussList会和之前的数据拼起来,isManagerScore为是否有打分权限，isMood与__hasData现在无用
  blogType: React.PropTypes.any,//用于生成CKEditor的唯一标识
  hasScroll: React.PropTypes.bool,//是否显示时间轴
  hasEditor: React.PropTypes.bool,//微博是否可编辑？
  hasImage: React.PropTypes.bool//是否显示头像
};
export default Weibos